//package com.freesky.web.manager.service.impl;
//
//import com.freesky.common.exception.EMallException;
//import com.freesky.common.pojo.DataTablesResult;
//import com.freesky.common.vo.VOMember;
//import com.freesky.common.dao.entity.Member;
//import com.freesky.web.manager.service.MemberService;
//import com.freesky.web.manager.util.VOUtil;
//import com.baomidou.mybatisplus.mapper.EntityWrapper;
//import com.baomidou.mybatisplus.plugins.Page;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Service;
//import org.springframework.util.DigestUtils;
//
//import java.util.Date;
//import java.util.List;
//
///**
// * @author Freesky
// * @date 2017/8/11
// */
//@Service
//public class MemberServiceImpl implements MemberService {
//
//    final static Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);
//
//    @Override
//    public Member getMemberById(long memberId) {
//
//        Member member = new Member();
//        member.setId(memberId);
//        member = member.selectById();
//        if (member == null) {
//            throw new EMallException("ID获取会员信息失败");
//        }
//        member.setPassword("");
//        return member;
//    }
//
//    @Override
//    public DataTablesResult getMemberList(int draw, int start, int length, String search,
//                                          String minDate, String maxDate, String orderCol, String orderDir) {
//
//        DataTablesResult result = new DataTablesResult();
//
//        try {
//            //分页
//            boolean asc = false;
//            if (orderDir.equalsIgnoreCase("asc")) {
//                asc = true;
//            }
//            Page<Member> memberPage = new Page<>();
//            search = "%" + search + "%";
//            if (minDate != null && minDate.length() > 0 && maxDate != null && maxDate.length() > 0) {
//                memberPage = new Member().selectPage(new Page<>(start / length + 1, length), new EntityWrapper<Member>()
//                        .ne("state", 2).and()
//                        .between("created", minDate, maxDate).and()
//                        .like("username", search).or()
//                        .like("phone", search).or()
//                        .like("email", search).or()
//                        .like("sex", search).or()
//                        .like("address", search)
//                        .orderBy(orderCol, asc));
//            } else {
//                memberPage = new Member().selectPage(new Page<>(start / length + 1, length), new EntityWrapper<Member>()
//                        .ne("state", 2).and()
//                        .like("username", search).or()
//                        .like("phone", search).or()
//                        .like("email", search).or()
//                        .like("sex", search).or()
//                        .like("address", search)
//                        .orderBy(orderCol, asc));
//            }
//
//            List<Member> list = memberPage.getRecords();
//            result.setRecordsFiltered(memberPage.getTotal());
//            result.setRecordsTotal(memberPage.getSize());
//
//            result.setDraw(draw);
//            result.setData(list);
//        } catch (Exception e) {
//            logger.error("加载用户列表失败",e);
//            throw new EMallException("加载用户列表失败");
//        }
//
//        return result;
//    }
//
//    @Override
//    public DataTablesResult getRemoveMemberList(int draw, int start, int length, String search,
//                                                String minDate, String maxDate, String orderCol, String orderDir) {
//
//        DataTablesResult result = new DataTablesResult();
//        search = "%" + search + "%";
//        try {
//            //分页
//            boolean asc = false;
//            if (orderDir.equalsIgnoreCase("asc")) {
//                asc = true;
//            }
//            Page<Member> memberPage = new Page<>();
//            if (minDate != null && minDate.length() > 0 && maxDate != null && maxDate.length() > 0) {
//                memberPage = new Member().selectPage(new Page<>(start / length + 1, length), new EntityWrapper<Member>()
//                        .ne("state", 2).and()
//                        .between("created", minDate, maxDate).and()
//                        .like("username", search).or()
//                        .like("phone", search).or()
//                        .like("email", search).or()
//                        .like("sex", search).or()
//                        .like("address", search)
//                        .orderBy(orderCol, asc));
//            } else {
//                memberPage = new Member().selectPage(new Page<>(start / length + 1, length), new EntityWrapper<Member>()
//                        .ne("state", 2).and()
//                        .like("username", search).or()
//                        .like("phone", search).or()
//                        .like("email", search).or()
//                        .like("sex", search).or()
//                        .like("address", search)
//                        .orderBy(orderCol, asc));
//            }
//            result.setRecordsFiltered(memberPage.getTotal());
//            result.setRecordsTotal(memberPage.getSize());
//
//            result.setDraw(draw);
//            result.setData(memberPage.getRecords());
//        } catch (Exception e) {
//            logger.error("加载删除用户列表失败",e);
//            throw new EMallException("加载删除用户列表失败");
//        }
//
//        return result;
//    }
//
//    @Override
//    public Member getMemberByUsername(String username) {
//        Member member = new Member().selectOne(new EntityWrapper().eq("username", username));
//        if (member==null) {
//            throw new EMallException("ID获取会员信息失败");
//        }else{
//            member.setPassword("");
//            return member;
//        }
//    }
//
//    @Override
//    public Member getMemberByPhone(String phone) {
//
//        Member member = new Member().selectOne(new EntityWrapper().eq("phone", phone));
//        if (member==null) {
//            throw new EMallException("Phone获取会员信息失败");
//        }else{
//            member.setPassword("");
//            return member;
//        }
//    }
//
//    @Override
//    public Member getMemberByEmail(String email) {
//        Member member = new Member().selectOne(new EntityWrapper().eq("email", email));
//        if (member==null) {
//            throw new EMallException("Phone获取会员信息失败");
//        }else{
//            member.setPassword("");
//            return member;
//        }
//    }
//
//    @Override
//    public DataTablesResult getMemberCount() {
//        DataTablesResult result = new DataTablesResult();
//        int count = new Member().selectCount(new EntityWrapper().eq("state", 2));
//        result.setRecordsTotal(count);
//        return result;
//    }
//
//    @Override
//    public DataTablesResult getRemoveMemberCount() {
//        DataTablesResult result = new DataTablesResult();
//        int count = new Member().selectCount(new EntityWrapper().eq("state", 2));
//        result.setRecordsTotal(count);
//        return result;
//    }
//
//    @Override
//    public Member addMember(VOMember voMember) {
//        Member member = VOUtil.changeVOMemberToMember(voMember);
//        if (getMemberByUsername(member.getUsername()) != null) {
//            throw new EMallException("用户名已被注册");
//        }
//        if (getMemberByPhone(member.getPhone()) != null) {
//            throw new EMallException("手机号已被注册");
//        }
//        if (getMemberByEmail(member.getEmail()) != null) {
//            throw new EMallException("邮箱已被注册");
//        }
//
//        member.setState(1);
//        member.setCreated(new Date());
//        member.setUpdated(new Date());
//        String md5Pass = DigestUtils.md5DigestAsHex(member.getPassword().getBytes());
//        member.setPassword(md5Pass);
//
//        boolean insert = member.insert();
//        if (!insert) {
//            throw new EMallException("添加用户失败");
//        }
//        return getMemberByPhone(member.getPhone());
//    }
//
//    @Override
//    public Member updateMember(Long id, VOMember voMember) {
//
//        Member member = VOUtil.changeVOMemberToMember(voMember);
//        member.setId(id);
//        member.setUpdated(new Date());
//        Member oldMember = getMemberById(id);
//        member.setState(oldMember.getState());
//        member.setCreated(oldMember.getCreated());
//        if (member.getPassword() == null || member.getPassword() == "") {
//            member.setPassword(oldMember.getPassword());
//        } else {
//            String md5Pass = DigestUtils.md5DigestAsHex(member.getPassword().getBytes());
//            member.setPassword(md5Pass);
//        }
//        boolean update = member.updateById();
//        if (!update) {
//            throw new EMallException("更新会员信息失败");
//        }
//        return member;
//    }
//
//    @Override
//    public Member changePassMember(Long id, VOMember voMember) {
//
//        Member member = new Member(id);
//        member=member.selectById();
//
//        String md5Pass = DigestUtils.md5DigestAsHex(voMember.getPassword().getBytes());
//        member.setPassword(md5Pass);
//        member.setUpdated(new Date());
//        boolean update = member.updateById();
//        if (!update) {
//            throw new EMallException("修改会员密码失败");
//        }
//        return member;
//    }
//
//    @Override
//    public Member alertMemberState(Long id, Integer state) {
//        Member member = new Member(id);
//        member=member.selectById();
//        member.setState(state);
//        member.setUpdated(new Date());
//        boolean update = member.updateById();
//        if (!update) {
//            throw new EMallException("修改会员状态失败");
//        }
//        return member;
//    }
//
//    @Override
//    public int deleteMember(Long id) {
//        Member member = new Member(id);
//        boolean delete = member.deleteById();
//        if (!delete) {
//            throw new EMallException("删除会员失败");
//        }
//        return 0;
//    }
//
//    @Override
//    public Member getMemberByEditEmail(Long id, String email) {
//        Member member = new Member(id);
//        member=member.selectById();
//        Member newMember = null;
//        if (member.getEmail() == null || !member.getEmail().equals(email)) {
//            newMember = getMemberByEmail(email);
//        }
//        newMember.setPassword("");
//        return newMember;
//    }
//
//    @Override
//    public Member getMemberByEditPhone(Long id, String phone) {
//        Member member = new Member(id);
//        member=member.selectById();
//        Member newMember = null;
//        if (member.getPhone() == null || !member.getPhone().equals(phone)) {
//            newMember = getMemberByPhone(phone);
//        }
//        newMember.setPassword("");
//        return newMember;
//    }
//
//    @Override
//    public Member getMemberByEditUsername(Long id, String username) {
//        Member member = new Member(id);
//        member=member.selectById();
//        Member newMember = null;
//        if (member.getUsername() == null || !member.getUsername().equals(username)) {
//            newMember = getMemberByUsername(username);
//        }
//        newMember.setPassword("");
//        return newMember;
//    }
//}
